package com.sumainfor.pc.gmio.controller.Excel;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sumainfor.common.easypoiExcel.utils.ExcelUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.gmio.dao.MktFeign.ManagerMapper;
import com.sumainfor.pc.gmio.entity.ReceiveRent;
import com.sumainfor.pc.gmio.service.impl.Excel.ExcelBillService;
import com.sumainfor.pc.gmio.service.impl.market.ManagerService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * Excel导出账单
 *
 * @Auther: zhlu
 * @Date: 2019/6/17 19:40
 * @Description:
 */
@RestController
@RequestMapping("/ExcelBill")
@Log4j2
public class ExcelBillController {

    @Autowired
    ExcelBillService ExcelBillService;

    /**
     * 功能描述:
     * Excel导出账单-收房租金
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/15 15:27
     */
    @RequestMapping(value = "/getExcelReceiveRent", method = RequestMethod.POST)
    public JsonResult getExcelReceiveRent(@RequestBody Map<String, Object> params)throws Exception {
        JsonResult result = new JsonResult();
        //时间处理
        Map<String,Object>DateDeal=DateDealwith(params);
        params.put("StartTime",DateDeal.get("StartTime"));
        params.put("EndTime",DateDeal.get("EndTime"));


        Pager pager = new Pager();
        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer count = ExcelBillService.getExcelReceiveRentCount(params);
        if (count == 0) {
            result = pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, count);
        } else {
            List<Map<String, Object>> ExcelReceiveRent = ExcelBillService.getExcelReceiveRent(params);
            result = pageUtils.getJsonResult(ExcelReceiveRent, params, count);
        }
        return result;
    }

    /**
     * 功能描述:
     * 导出收房租金统计表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 11:22
     */
    @GetMapping("/ExcelReceiveRent")
    public void ExcelReceiveRent(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //时间处理
        Map<String,Object>DateDeal=DateDealwith(params);
        params.put("StartTime",DateDeal.get("StartTime"));
        params.put("EndTime",DateDeal.get("EndTime"));

        //公司集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"areaList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "areaList"), String.class);
            params.put("areaList",Array);
        }
        //服务中心集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"deptList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "deptList"), String.class);
            params.put("deptList",Array);
        }
        //项目集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"comList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "comList"), String.class);
            params.put("comList",Array);
        }
        //楼号集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"HouList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "HouList"), String.class);
            params.put("HouList",areaArray);
        }
        //房号集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"houIdList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "houIdList"), String.class);
            params.put("houIdList",areaArray);
        }
        //室号集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"houinfoList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "houinfoList"), String.class);
            params.put("houinfoList",areaArray);
        }
        //构建第一个sheet
        Map<String, Object> map1 = ExcelBillService.ExcelReceiveRent(params);
        log.info("map1-----------" + map1);
        List<Map<String, Object>> datelist = new ArrayList<Map<String, Object>>();
        datelist.add(map1);

        //构建第二个sheet
        Map<String, Object> map2 = ExcelBillService.ExcelReceiveRentInfo(params);
        datelist.add(map2);
        //导出
        Workbook workbook = ExcelUtils.exportExcel(datelist, ExcelType.HSSF);
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            String filedisplay = "房租收款统计表.xls";
            filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:
     * 时间查询处理
     * 月份定义：上月25日00：00到本月24日00:00
     * 前端。传递2019-01至2019-05
     * 转换之后。应该查询的数据是2019-01-25-2019-05-24之间的数据
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 15:14
     */
    public Map<String, Object> DateDealwith(Map<String, Object> params)throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        //月份开始时间
        String monthStartTime;
        //月份结束时间
        String monthEndTime;
        //如果开始月份为空，默认当年1月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"monthStartTime"})){
            DateTime date = ConvertDateTime.dateFormatStrToTime("yyyy-MM",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-01");
            monthStartTime= date.toString("yyyy-MM");
        }else{
            DateTime date =ConvertDateTime.dateFormatStrToTime("yyyy-MM",params.get("monthStartTime").toString());
            monthStartTime= date.toString("yyyy-MM");
        }
        //如果结束月份为空，默认当年12月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"monthEndTime"})){
            DateTime date = ConvertDateTime.dateFormatStrToTime("yyyy-MM",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-12");
            monthEndTime=date.toString("yyyy-MM");
        }else{
            DateTime date =ConvertDateTime.dateFormatStrToTime("yyyy-MM",params.get("monthEndTime").toString());
            monthEndTime=date.toString("yyyy-MM");
        }

        result.put("StartTime", monthStartTime + "-25");
        result.put("EndTime", monthEndTime + "-24");
        return result;
    }


    /**
     * 功能描述:
     * 房租支付统计表列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/15 15:27
     */
    @RequestMapping(value = "/getExcelReceiveOwnerRent", method = RequestMethod.POST)
    public JsonResult getExcelReceiveOwnerRent(@RequestBody Map<String, Object> params)throws Exception {
        JsonResult result = new JsonResult();
        //时间处理
        Map<String,Object>DateDeal=DateDealwith(params);
        params.put("StartTime",DateDeal.get("StartTime"));
        params.put("EndTime",DateDeal.get("EndTime"));

        Pager pager = new Pager();
        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer count = ExcelBillService.getExcelReceiveOwnerRentCount(params);
        if (count == 0) {
            result = pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, count);
        } else {
            List<Map<String, Object>> ExcelReceiveOwnerRent = ExcelBillService.getExcelReceiveOwnerRent(params);
            result = pageUtils.getJsonResult(ExcelReceiveOwnerRent, params, count);
        }
        return result;
    }


    /**
     * 功能描述:
     * 导出收房租金统计表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/18 11:22
     */
    @GetMapping("/ExcelReceiveOwnerRent")
    public void ExcelReceiveOwnerRent(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //时间处理
        Map<String,Object>DateDeal=DateDealwith(params);
        params.put("StartTime",DateDeal.get("StartTime"));
        params.put("EndTime",DateDeal.get("EndTime"));

        //公司集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"areaList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "areaList"), String.class);
            params.put("areaList",Array);
        }
        //服务中心集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"deptList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "deptList"), String.class);
            params.put("deptList",Array);
        }
        //项目集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"comList"})) {
            List<String> Array = JSONObject.parseArray(MapUtils.getString(params, "comList"), String.class);
            params.put("comList",Array);
        }
        //楼号集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"HouList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "HouList"), String.class);
            params.put("HouList",areaArray);
        }
        //房号集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"houIdList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "houIdList"), String.class);
            params.put("houIdList",areaArray);
        }
        //房源详表集合处理
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"houinfoList"})) {
            List<String> areaArray = JSONObject.parseArray(MapUtils.getString(params, "houinfoList"), String.class);
            params.put("houinfoList",areaArray);
        }
        //构建第一个sheet
        Map<String, Object> map1 = ExcelBillService.ExcelReceiveOwnerRent(params);
        log.info("map1-----------" + map1);
        List<Map<String, Object>> datelist = new ArrayList<Map<String, Object>>();
        datelist.add(map1);

        //构建第二个sheet
        Map<String, Object> map2 = ExcelBillService.ExcelReceiveOwnerRentInfo(params);
        datelist.add(map2);
        //导出
        Workbook workbook = ExcelUtils.exportExcel(datelist, ExcelType.HSSF);
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            String filedisplay = "房租支付统计表.xls";
            filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
